package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderSignBean;
import com.koron.order.common.mapper.OrderSignMapper;
import com.koron.order.common.service.OrderSignService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (OrderSign)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-16 14:54:21
 */
@Service("orderSignService")
public class OrderSignServiceImpl implements OrderSignService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderSignBean orderSign) {

        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);
        if (StringUtils.isEmpty(orderSign.getId())) {
            orderSign.setId(CodeTools.getCode32());
        }

        return mapper.insert(orderSign);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderSignBean> orderSignBeanList) {

        if (CollectionUtils.isEmpty(orderSignBeanList)) {
            return 0;
        }
        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);
        List<String> itemIds = orderSignBeanList.stream().map(OrderSignBean::getItemId).distinct().collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(itemIds)) {
            mapper.deleteByItemIds(itemIds);
        }

        return mapper.insertBatch(orderSignBeanList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderSignBean orderSign) {
        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);
        return mapper.update(orderSign);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderSignBean queryById(SessionFactory factory, String id) {

        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderSignBean orderSign, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);

        List<OrderSignBean> list = mapper.queryList(orderSign);

        return new PageInfo(list);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderSignBean> queryList(SessionFactory factory, OrderSignBean orderSign) {
        OrderSignMapper mapper = factory.getMapper(OrderSignMapper.class);

        List<OrderSignBean> list = mapper.queryList(orderSign);

        return list;
    }

}